import {request} from "@umijs/max";
import {ResponseStructure} from "@/requestErrorConfig";
import {API, DynamicObject, PageParams} from "@/services/typings";
import {deleteById, deleteData, post} from "@/services/base-request";

export async function pageRole(
  params: PageParams,
  options?: { [key: string]: any },
) {
  return request<ResponseStructure>('/api/role/page', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

export async function updateRoleById(role: API.IRole, options?: DynamicObject,) {
  return request<ResponseStructure>('/api/role/updateById', {
    method: 'put',
    data: role,
    ...(options || {}),
  });
}


export async function addRole(role: API.IRole, options?: DynamicObject,) {
  return request<ResponseStructure>('/api/role', {
    method: 'POST',
    data: role,
    ...(options || {}),
  });
}

export async function deleteRole(id: string | number, options?: DynamicObject) {
  return request<ResponseStructure>(`/api/role/${id}`, {
    method: 'DELETE',
    ...(options || {}),
  });
}

export async function assignPermissions(roleId: string | number, menuIds: string[], options?: DynamicObject) {
  return request<ResponseStructure>(`/api/role/assignPermissions`, {
    method: 'POST',
    data: {
      id: roleId,
      menuIds
    },
    ...(options || {}),
  });
}

export const deleteUserRoleAPI = (roleId: string, userId: string, options?: DynamicObject) => {
  return deleteData(`/role/deleteByUser`, {
    id: roleId,
    userId
  }, options)
}

export const addUserRoleAPI = (roleId: string, userId: string, options?: DynamicObject) => {
  return post(`/role/addUserRole`, {
    id: roleId,
    userId
  }, options)
}
